package is.billsharing.app;

import java.util.ArrayList;

import org.apache.http.NameValuePair;
import org.apache.http.message.BasicNameValuePair;

import android.app.Activity;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemSelectedListener;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Spinner;
import android.widget.TextView;

	
public class AddGrpMem extends Activity implements OnItemSelectedListener{
	private static final String PREFS_NAME = "user";
	int uid,gid;
	EditText fnd_email_grp;
	Button addUserToGroup,deleteUserFromGroup;
	TextView tv;
	String[] friends;
	String userEmail;
	@Override
    public void onCreate(Bundle savedInstanceState) 
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.grpmems);
        addUserToGroup = (Button) findViewById(R.id.grp_add_fnd);
		deleteUserFromGroup = (Button) findViewById(R.id.grp_del_fnd);
		tv = (TextView) findViewById(R.id.textView1);
		SharedPreferences settings = getSharedPreferences(PREFS_NAME, 0);
		uid = settings.getInt("uid", 0);
		gid = settings.getInt("gid", 0);
		ArrayList<NameValuePair> friendParameters = new ArrayList<NameValuePair>();
		friendParameters.add(new BasicNameValuePair("uid", "" + uid + ""));
		try {
			String response = CustomHttpClient.executeHttpPost("Friends",friendParameters);
			response = response.replace("[", "");
			response = response.replace("]", "");
			friends = response.split(",");
			tv.setText(response);
		} catch (Exception e) {
			e.printStackTrace();
		}

		final Spinner spin = (Spinner) findViewById(R.id.friends_list);
		spin.setOnItemSelectedListener(this);
	

		ArrayAdapter aa = new ArrayAdapter(this,android.R.layout.simple_spinner_item, friends);

		aa.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
		spin.setAdapter(aa);

		addUserToGroup.setOnClickListener(new View.OnClickListener() {
			
			public void onClick(View v) {
				
			//	String userEmail = spin.getSelectedItem().toString();
				tv.setText(userEmail+gid);
				ArrayList<NameValuePair> addFriendParameters = new ArrayList<NameValuePair>();
				addFriendParameters.add(new BasicNameValuePair("gid", "" + gid + ""));
				addFriendParameters.add(new BasicNameValuePair("userEmail",userEmail));
				try
				{
				String response = CustomHttpClient.executeHttpPost("AddUserToGroup",addFriendParameters);
				}
				catch(Exception e)
				{
					e.printStackTrace();
				}
				
			}
		});

    }
	
	
	public void onItemSelected(AdapterView<?> parent, View v, int position,long id) {
		userEmail = (String) parent.getItemAtPosition(position);


	}

	public void onNothingSelected(AdapterView<?> parent) {
		tv.setText("");

	}


}
